package com.xxx.games.truthOrDare;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.xxx.games.BR;
import com.xxx.games.R;
import com.xxx.games.addPlayer.AddPlayerActivity;
import com.xxx.games.bean.TagBean;
import com.xxx.games.app.Constant;
import com.xxx.games.databinding.ActivityTruthOrDareBinding;
import com.xxx.games.utils.FileUtils;
import com.xxx.games.widget.ITurntableListener;
import com.xxx.games.widget.ShowTruthOrDareDialog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;

/**
 * 真心话大冒险
 */
public class TruthOrDareActivity extends BaseActivity<ActivityTruthOrDareBinding, BaseViewModel> {

    private TagBean players;
    private ShowTruthOrDareDialog dareDialog;
    private List<String> quas;
    private List<String> dos;
    private TagBean quasAcache;
    private TagBean dosAcache;
    private int num;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_truth_or_dare;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        //获取本地存储的数据
        players = mmkv.decodeParcelable(Constant.MMKV_KEY_PLAYERS, TagBean.class);
        //初始化数据
        initQua();
        initDo();
    }

    public void initQua() {
        quasAcache = mmkv.decodeParcelable(Constant.MMKV_KEY_QUAS, TagBean.class);


        if (quasAcache != null && quasAcache.getQuado() != null && quasAcache.getQuado().size() > 0) {
            return;
        }

        try {
            String str = FileUtils.file2String("qua.txt");
            quas = Arrays.asList(str.split("\\?"));
            List<TagBean> temp = new ArrayList<>();
            for (String qua : quas) {
                temp.add(new TagBean(qua));
            }
            TagBean tagBean = new TagBean();
            tagBean.setQuado(temp);
            //储存到本地
            mmkv.encode("quas", tagBean);
            quasAcache = tagBean;
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public void initDo() {
        dosAcache = mmkv.decodeParcelable(Constant.MMKV_KEY_DOS, TagBean.class);

        if (dosAcache != null && dosAcache.getQuado() != null && dosAcache.getQuado().size() > 0) {
            return;
        }
        try {
            String str = FileUtils.file2String("do.txt");
            dos = Arrays.asList(str.split("~"));
            List<TagBean> temp = new ArrayList<>();
            for (String todo : dos) {
                temp.add(new TagBean(todo));
            }
            TagBean tagBean = new TagBean();
            tagBean.setQuado(temp);
            //储存到本地
            mmkv.encode("dos", tagBean);
            dosAcache = tagBean;
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void initData() {
        super.initData();
        if (players != null && players.getTags() != null && players.getTags().size() > 0) {
            ArrayList<String> names = new ArrayList<>();
            ArrayList<Integer> colors = new ArrayList<>();
            for (TagBean tag : players.getTags()) {
                names.add(tag.getTag());
                colors.add(tag.getPlaceholderRes());
            }
            binding.turntable.setDatas(names, colors);
        } else {
            initViewData();
        }

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        binding.tvBack.setOnClickListener(lis -> {
            finish();
        });
        binding.tvAdd.setOnClickListener(lis -> {
            startActivityForResult(new Intent(TruthOrDareActivity.this, AddPlayerActivity.class), 100);
        });
        binding.tvAddQua.setOnClickListener(lis -> {
            startActivityForResult(new Intent(TruthOrDareActivity.this, AddQuasActivity.class), 100);
        });
        binding.tvAddDoit.setOnClickListener(lis -> {
            startActivityForResult(new Intent(TruthOrDareActivity.this, AddDosActivity.class), 100);
        });

        binding.ivNode.setOnClickListener(lis -> {
            binding.turntable.startRotate(new ITurntableListener() {
                @Override
                public void onStart() {
                    num++;
                    binding.tvNum.setText(String.valueOf(num));
                }

                @Override
                public void onEnd(int position, String name) {
                    dareDialog = new ShowTruthOrDareDialog(TruthOrDareActivity.this, name, quasAcache.getQuado(), dosAcache.getQuado());
                    dareDialog.showDialog();
                }
            });
        });
    }

    private void initViewData() {
        int num = 6;
        ArrayList<String> names = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            names.add((i + 1) + "号玩家");
        }
        binding.turntable.setDatas(num, names);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {
            if (resultCode == 101) {
                //重新加载转盘
                players = (TagBean) data.getParcelableExtra("tags");
                initData();
            } else if (resultCode == 102) {
                //重新去取真心话
                quasAcache = (TagBean) data.getParcelableExtra("tags");
            } else if (resultCode == 103) {
                //重新去取大冒险
                dosAcache = (TagBean) data.getParcelableExtra("tags");
            }
        }

    }
}